package com.github.wxiaoqi.security.gate.utils;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import com.github.wxiaoqi.security.auth.client.jwt.ServiceAuthUtil;
import com.github.wxiaoqi.security.common.context.BaseContextHandler;
import com.netflix.zuul.context.RequestContext;

import lombok.extern.slf4j.Slf4j;

/**
 * @author Mu
 * @Description 
 * 请求路径过滤
 * */
@Configuration
@Slf4j
public class FilterUrlUtils {
	
	/**
	 * 网关忽略的地址
	 * */
	@Value("${gate.ignore.startWiths}")
	private  String ignoreUrls;
	/**
	 * 请求路径起初地址
	 * */
	@Value("${zuul.prefix}")
	private  String zuulPrefix;
	/**
	 * 网关拦截的对外提供的服务地址
	 * */
	@Value("${gate.intercept_Urls}")
	private  String interceptUrls;
	
	/**
	 * 过滤地址
	 * @param type 0: 表示过滤不需要走网关的地址；1: 表示过滤对外提供服务地址；
	 * @return
	 */
	public  boolean filterStartWithUrl(int type) {
		RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        final String requestUrl = request.getRequestURI().substring(zuulPrefix.length());
        BaseContextHandler.setToken(null);
		boolean flag = false;
		if(0==type){
			for (String s : ignoreUrls.split(",")) {
				if (requestUrl.startsWith(s)) {
					return true;
				}
			}
		}
		if(1==type){
			for (String s : interceptUrls.split(",")) {
				if (requestUrl.startsWith(s)) {
					return true;
				}
			}
		}
		return flag;
	}
	
	
}
